export function setGlobalVxeTableThemeCSSVariables(color = '#0CB58E') {
  function hexToHSL(hex) {
    const r = Number.parseInt(hex.substring(1, 3), 16) / 255
    const g = Number.parseInt(hex.substring(3, 5), 16) / 255
    const b = Number.parseInt(hex.substring(5, 7), 16) / 255

    const max = Math.max(r, g, b)
    const min = Math.min(r, g, b)
    let h; let s; const l = (max + min) / 2

    if (max === min) {
      h = s = 0 // achromatic
    }
    else {
      const d = max - min
      s = l > 0.5 ? d / (2 - max - min) : d / (max + min)
      switch (max) {
        case r:
          h = (g - b) / d + (g < b ? 6 : 0)
          break
        case g:
          h = (b - r) / d + 2
          break
        case b:
          h = (r - g) / d + 4
          break
      }
      h /= 6
    }

    return [h * 360, s * 100, l * 100] // HSL
  }

  function adjustHSL(h, s, l, hOffset = 0, sOffset = 0, lOffset = 0) {
    return [
      (h + hOffset + 360) % 360, // Ensure h is in range [0, 360]
      Math.min(Math.max(s + sOffset, 0), 100), // Saturation should be between 0 and 100
      Math.min(Math.max(l + lOffset, 0), 100), // Lightness should be between 0 and 100
    ]
  }

  function HSLToHex(h, s, l) {
    s /= 100
    l /= 100
    const c = (1 - Math.abs(2 * l - 1)) * s
    const x = c * (1 - Math.abs(((h / 60) % 2) - 1))
    const m = l - c / 2
    let r, g, b

    if (h >= 0 && h < 60) {
      r = c; g = x; b = 0
    }
    else if (h >= 60 && h < 120) {
      r = x; g = c; b = 0
    }
    else if (h >= 120 && h < 180) {
      r = 0; g = c; b = x
    }
    else if (h >= 180 && h < 240) {
      r = 0; g = x; b = c
    }
    else if (h >= 240 && h < 300) {
      r = x; g = 0; b = c
    }
    else {
      r = c; g = 0; b = x
    }

    return `#${Math.round((r + m) * 255).toString(16).padStart(2, '0')}${Math.round((g + m) * 255).toString(16).padStart(2, '0')}${Math.round((b + m) * 255).toString(16).padStart(2, '0')}`
  }
  function generateColorVariants(baseColor) {
    const [h, s, l] = hexToHSL(baseColor)

    // 生成不同变体
    const lightColor = HSLToHex(...adjustHSL(h, s, l, 0, 0, 20)) // 亮色
    const darkColor = HSLToHex(...adjustHSL(h, s, l, 0, 0, -20)) // 暗色
    const hoverColor = HSLToHex(...adjustHSL(h, s, l, 0, 0, -10)) // 悬停色
    const tingeColor = HSLToHex(...adjustHSL(h, s + 20, l)) // 变色
    const disabledColor = HSLToHex(...adjustHSL(h, s - 30, l + 10)) // 禁用色

    return {
      lightColor,
      darkColor,
      hoverColor,
      tingeColor,
      disabledColor,
    }
  }

  const baseColor = color // 传入你想要的颜色
  const colorVariants = generateColorVariants(baseColor)

  function setGlobalCSSVariables(colorVariants) {
    const root = document.documentElement

    root.style.setProperty('--vxe-ui-font-primary-color', colorVariants.lightColor)
    root.style.setProperty('--vxe-ui-font-primary-hover-color', colorVariants.hoverColor)
    root.style.setProperty('--vxe-ui-font-primary-tinge-color', colorVariants.tingeColor)
    root.style.setProperty('--vxe-ui-font-primary-lighten-color', colorVariants.lightColor)
    root.style.setProperty('--vxe-ui-font-primary-darken-color', colorVariants.darkColor)
    root.style.setProperty('--vxe-ui-font-primary-disabled-color', colorVariants.disabledColor)
  }

  setGlobalCSSVariables(colorVariants)
}
